package com.glassshop.ui;

import com.glassshop.R;
import com.glassshop.adapter.GuideAdapter;
import com.glassshop.adapter.PictureAdapter;
import com.glassshop.app.AppContext;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends Activity implements ViewPager.OnPageChangeListener{
	
	private AppContext appContext;// 全局Context
    private List<View> views;
    private PictureAdapter pictureAdapter;
    private ViewPager viewPager;
    private ImageView[] dots;
    private int currentIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);   
        
        appContext = (AppContext) getApplication();
        initView();
        initDot();
    }

    private void  initView(){
        LayoutInflater inflater = LayoutInflater.from(this);
        viewPager = (ViewPager)findViewById(R.id.picture_viewpager);

        views = new ArrayList<View>();
        views.add(inflater.inflate(R.layout.picture1, null));
        views.add(inflater.inflate(R.layout.picture2, null));
        views.add(inflater.inflate(R.layout.picture3, null));
        views.add(inflater.inflate(R.layout.picture4, null));
        views.add(inflater.inflate(R.layout.picture5, null));

        pictureAdapter = new PictureAdapter(views, this);
        viewPager.setAdapter(pictureAdapter);
        viewPager.setOnPageChangeListener(this);

    }

    private void initDot(){
        LinearLayout ll = (LinearLayout) findViewById(R.id.ll_picture_viewpager);

        dots = new ImageView[views.size()];

        // 循环取得小点图片
        for (int i = 0; i < views.size(); i++) {
            dots[i] = (ImageView) ll.getChildAt(i);
            dots[i].setEnabled(true);// 都设为灰色
        }

        viewPager.setCurrentItem(0);
        dots[currentIndex].setEnabled(false);// 设置为白色，即选中状态
    }

    @Override
    public void onPageScrolled(int i, float v, int i2) {

    }

    @Override
    public void onPageSelected(int position) {
        position = position % views.size();
        setCurrentDot(position);
    }

    @Override
    public void onPageScrollStateChanged(int i) {
        if(i == ViewPager.SCROLL_STATE_IDLE) {
                             if(currentIndex == pictureAdapter.getCount() - 1) {
                                     viewPager.setCurrentItem( 1, false);
                                 }
                             else if(currentIndex == 0) {
                                 viewPager.setCurrentItem( pictureAdapter.getCount() - 2, false);
                                 }
                         }
    }

    private void setCurrentDot(int position) {
        // TODO Auto-generated method stub
        if (position < 0 || position > views.size() - 1
                || currentIndex == position) {
            return;
        }

        dots[position].setEnabled(false);
        dots[currentIndex].setEnabled(true);

        currentIndex = position;
    }
}
